package fun.boomcat.luckyhe.mirai.plugin.service;

import fun.boomcat.luckyhe.mirai.plugin.dao.CommandMappingMapper;
import fun.boomcat.luckyhe.mirai.plugin.pojo.CommandMapping;
import fun.boomcat.luckyhe.mirai.plugin.utils.RconCommandMyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class CommandMappingServiceImpl implements CommandMappingService{
    @Override
    public int createCommandMappingTable() throws IOException {
        SqlSession sqlSession = RconCommandMyBatisUtil.getSqlSession();
        CommandMappingMapper mapper = sqlSession.getMapper(CommandMappingMapper.class);
        int res = mapper.createCommandMappingTable();
        sqlSession.commit();
        sqlSession.close();

        return res;
    }

    @Override
    public int insertOneCommandMapping(CommandMapping commandMapping) throws IOException {
        SqlSession sqlSession = RconCommandMyBatisUtil.getSqlSession();
        CommandMappingMapper mapper = sqlSession.getMapper(CommandMappingMapper.class);
        int res = mapper.insertOneCommandMapping(commandMapping);
        sqlSession.commit();
        sqlSession.close();

        return res;
    }

    @Override
    public CommandMapping selectSameSourceCommandMapper(String source) throws IOException {
        SqlSession sqlSession = RconCommandMyBatisUtil.getSqlSession();
        CommandMappingMapper mapper = sqlSession.getMapper(CommandMappingMapper.class);
        CommandMapping commandMapping = mapper.selectSameSourceCommandMapper(source);
        sqlSession.close();

        return commandMapping;
    }

    @Override
    public int delOneCommandMapping(CommandMapping commandMapping) throws IOException {
        SqlSession sqlSession = RconCommandMyBatisUtil.getSqlSession();
        CommandMappingMapper mapper = sqlSession.getMapper(CommandMappingMapper.class);
        int res = mapper.delOneCommandMapping(commandMapping);
        sqlSession.commit();
        sqlSession.close();

        return res;
    }

    @Override
    public List<CommandMapping> selectAllCommandMapping() throws IOException {
        SqlSession sqlSession = RconCommandMyBatisUtil.getSqlSession();
        CommandMappingMapper mapper = sqlSession.getMapper(CommandMappingMapper.class);
        List<CommandMapping> commandMappings = mapper.selectAllCommandMapping();
        sqlSession.close();

        return commandMappings;
    }

    @Override
    public List<CommandMapping> selectMatchCommandMapping(String source) throws IOException {
        SqlSession sqlSession = RconCommandMyBatisUtil.getSqlSession();
        CommandMappingMapper mapper = sqlSession.getMapper(CommandMappingMapper.class);
        List<CommandMapping> commandMappings = mapper.selectMatchCommandMapping(source);
        sqlSession.close();

        return commandMappings;
    }
}
